/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import org.crystalrange.documents.model.DocumentCategory;
import org.crystalrange.documents.service.DocumentCategoryService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author Krzysiek
 */
public class DocumentCategorySelectController extends GenericForwardComposer {
    
    protected DocumentCategoryService documentCategoryService;
    protected DocumentCategory selectedDocumentCategory;
    protected List<DocumentCategory> documentCategorys;
    protected Search searchDocumentCategory;
    protected AnnotateDataBinder binder;

    public DocumentCategoryService getDocumentCategoryService() {
        return documentCategoryService;
    }

    public void setDocumentCategoryService(DocumentCategoryService documentCategoryService) {
        this.documentCategoryService = documentCategoryService;
    }

    public List<DocumentCategory> getDocumentCategorys() {
        return documentCategorys;
    }

    public void setDocumentCategorys(List<DocumentCategory> documentCategorys) {
        this.documentCategorys = documentCategorys;
    }

    public DocumentCategory getSelectedDocumentCategory() {
        return selectedDocumentCategory;
    }

    public void setSelectedDocumentCategory(DocumentCategory selectedDocumentCategory) {
        this.selectedDocumentCategory = selectedDocumentCategory;
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        this.searchDocumentCategory = new Search(DocumentCategory.class);
        this.searchDocumentCategory.setMaxResults(100);
        this.searchDocumentCategory.addFetch("createdBy");
        this.documentCategorys = this.documentCategoryService.search(this.searchDocumentCategory);
        component.setAttribute("controller", this);
    }

    public void onClick$buttonOk(Event event) throws Exception {
        if (this.selectedDocumentCategory != null) {
            this.execution.setAttribute("selectedDocumentCategory", this.selectedDocumentCategory);
            this.execution.setAttribute("result", "ok");
            this.self.detach();
        } else {
            Messagebox.show("Please select a Document Category.");
        }
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("selectedDocumentCategory", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }
    
    public void onClick$buttonSearch(Event event) throws Exception {
        this.execution.setAttribute("searchDocumentCategory", this.searchDocumentCategory);

        Window window = (Window) this.execution.createComponents("~./zul/html/documentCategorySearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDocumentCategory = (Search) this.execution.getAttribute("searchDocumentCategory");
        }

        this.documentCategorys = this.documentCategoryService.search(this.searchDocumentCategory);
        this.binder.loadAll();
    }
}
